package ui.label;

import ui.common.CommonButton;
import ui.common.CommonJTextField;
import ui.common.CommonLabel;
import ui.panel.ClickPanel;
import ui.panel.WorkPanel;
import utils.CommonUtil;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class CoordinateLabel extends JLabel {

    private int width = WorkPanel.width-490;
    public CommonLabel nameLabel = new CommonLabel("", JLabel.RIGHT);
    private CommonLabel xlLabel = new CommonLabel("xL ", JLabel.RIGHT);
    private CommonLabel xrLabel = new CommonLabel("xR ", JLabel.RIGHT);
    private CommonLabel yuLabel = new CommonLabel("yU ", JLabel.RIGHT);
    private CommonLabel ydLabel = new CommonLabel("yD ", JLabel.RIGHT);
    public CommonJTextField xlField = new CommonJTextField(25);
    public CommonJTextField xrField = new CommonJTextField(25);
    public CommonJTextField yuField = new CommonJTextField(25);
    public CommonJTextField ydField = new CommonJTextField(25);
    public ClickPanel clickPanel;
    public int symbol;
    public int y;
    public int i = -1;

    public CoordinateLabel(int y, int symbol, String name, ClickPanel clickPanel) {
        this.y = y;
        this.symbol = symbol;
        this.nameLabel.setText(name+" :");
        this.clickPanel = clickPanel;
        setLabel(y);
    }

    private void setLabel(int y) {

        Font font = CommonUtil.getFont();
//        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setFont(font);
        this.setForeground(Color.WHITE);
        this.setBounds(310,y,WorkPanel.width-20,30);
        nameLabel.setBounds(10,1,65,28);
        xlLabel.setBounds(70+(width/5+30)*0,1,30,28);
        xrLabel.setBounds(70+(width/5+30)*1,1,30,28);
        yuLabel.setBounds(70+(width/5+30)*2,1,30,28);
        ydLabel.setBounds(70+(width/5+30)*3,1,30,28);
        xlField.setBounds(100+(width/5+30)*0,1,width/5,28);
        xrField.setBounds(100+(width/5+30)*1,1,width/5,28);
        yuField.setBounds(100+(width/5+30)*2,1,width/5,28);
        ydField.setBounds(100+(width/5+30)*3,1,width/5,28);
        this.add(nameLabel);
        this.add(xlLabel);
        this.add(xrLabel);
        this.add(yuLabel);
        this.add(ydLabel);
        this.add(xlField);
        this.add(xrField);
        this.add(yuField);
        this.add(ydField);
        CommonButton registerBtn = new CommonButton(ImageUtils.registerImg,100+(width/5+30)*3+15+width/5,0,32,32);
        registerBtn.addActionListener(e -> registerData());
        this.add(registerBtn);

    }

    public void setHeight(int height){
        this.setBounds(5,y,WorkPanel.width-20,height);
        refresh();
    }

    public void registerData(){
        xlField.setText(clickPanel.rectDraw.orgx+"");
        xrField.setText(clickPanel.rectDraw.endx+"");
        yuField.setText(clickPanel.rectDraw.orgy+"");
        ydField.setText(clickPanel.rectDraw.endy+"");
    }

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}

